home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Ham Radio 2000
/
Ham Radio 2000.iso
/
ham2000
/
satellit
/
im2gif
/
spec.c
< prev
next >
Wrap
Text File
|
1991-11-24
|
4KB
|
195 lines
/* Remove speckles from uosat pics
21SEP91 NK6K
Use after inter. Usage:
spec infile outfile [cutoff]
Get a line. See if a pixel is more than 20 different from its neighbors.
If so, make it the average of its neighbors.
Search for white line by looking in black level area for pixels greater
than cutoff, which defaults to 60. Adjust the average of any such line
to the average of the last non-white line. If the line after a white line
is not a white line, and is darker than the previous non-white-line average,
adjust it up to the previous average.
The will make the bar be less objectionable.
*/
#include <stdio.h>
#include <dos.h>
#include <fcntl.h>
#include <sys\types.h>
#include <sys\stat.h>
#define LINE_SIZE 612
#define NUM_LINE_FIELD 288
long start_field_2;
int lines=0;
int skip_lines=0;
int correct;
int correct_fudge=0;
long doubles=0;
int bar;
unsigned char buff[LINE_SIZE+1];
int fillval=0;
long fixed=0;
int correct_array[10] = {0,5,5,5,5,5,5,5,5,5};
int cutoff;
int cur_bar=0;
int black=0;
main(argc,argv)
int argc;
char *argv[];
{
int fi1,fi2,fo;
int i;
long average,averageo;
int averagei;
if (argc<2) {
printf ("usage: spec input output val\n");
exit(1);
}
if ((fi1= open(argv[1], O_BINARY)) == -1){
printf("cannot open: %s\n",argv[1]);
perror("On input file");
exit(1);
}
if ((fo = open(argv[2], 0)) != -1){
printf("output file exists. Aborted\n");
exit(1);
}
if ((fo = open(argv[2],O_CREAT | O_TRUNC | O_BINARY | O_RDWR, S_IREAD | S_IWRITE)) == -1) {
printf("cannot open: %s\n",argv[2]);
perror("On output file");
exit(1);
}
cutoff=60;
if (argc>3) cutoff = atoi(argv[3]);
printf("bar detect if > %u\n",cutoff);
while (1) { /* Do till there isn't any more */
if (read(fi1, buff, LINE_SIZE)<LINE_SIZE) {
break;
}
averageo=average;
averagei=average=0l;
for (i=45;i<LINE_SIZE-1;i++) {
average+=buff[i];
averagei++;
if ( (abs((int)buff[i]-(int)buff[i-1])>20) &&
(abs((int)buff[i]-(int)buff[i+1])>20)) {
/*printf("line %u:%u (%x %x %x)\n",lines,i,
buff[i-1],buff[i],buff[i+1]);*/
/* check to see if the next pixel isn't a better
candidate */
if ( ( abs((int)buff[i+1]-(int)buff[i-1]) ) >
( abs((int)buff[i]-(int)buff[i-1]))) {
doubles++;
}
else {
fixed++;
buff[i] = ((unsigned int)buff[i-1]+(unsigned int)buff[i+1])/2;
}
}
}
average = average/averagei;
/* if (buff[34]!=0) {*/
#ifdef OLD
if (buff[34]>50) {
correct=buff[34];
for (i=0;i<10;i++) correct+=buff[34+i];
correct/=10;
correct+=correct_array[correct_fudge];
correct_fudge++;
for (i=34;i<LINE_SIZE-1;i++) {
if ( ((int) buff[i] - (int) correct)>0)
buff[i] = (int) buff[i] - (int) correct ;
else buff[i]=0;
}
}
#else
if ((buff[34]>cutoff) || (buff[36]>cutoff)) {
/* subtract the difference between the average
of this line and the average of the previous
line. */
cur_bar=1;
bar++;
correct = average-averageo;
average=averageo;
for (i=34;i<LINE_SIZE-1;i++) {
if ( ((int) buff[i] - (int) correct)>0)
buff[i] = (int) buff[i] - (int) correct ;
else buff[i]=0;
}
for (i=0;i<20;i++) buff[i]=255;
}
else {
if (cur_bar) cur_bar++;
if (cur_bar && (cur_bar<9)) {
/* if we're within 9 lines of the
bar, and this line is some limit
darker than the previous average,
lighten it. */
if ((average-averageo)<-2) {
black++;
correct = averageo-average;
average=averageo;
for (i=34;i<LINE_SIZE-1;i++) {
if ( ((int) buff[i] + (int) correct)<255)
buff[i] = (int) buff[i] + (int) correct ;
else buff[i]=255;
}
for (i=0;i<20;i++) buff[i]=128;
}
}
}
#endif
if (write(fo,buff,LINE_SIZE)==-1) {
perror("On output file\n");
exit(1);
}
/* printf("Line %u fixed %u \r",lines,fixed);*/
lines++;
}
printf("Fixed %lu, doubles %lu\n",fixed,doubles);
printf("White bar found on %u lines\n",bar);
printf("Black bar found on %u lines\n",black);
close (fo);
close (fi1);
}